$(document).ready(function () {
    form = $('#charge_form');
    var $form=new xhForm(form);
    $form.parse();
    $form.bindRequired($('#bt_modal_confirm',parent.document));
    bindTypeSelect();//点击型号

    function bindTypeSelect() {
        if (free_data === 1){//收费
            $('#project').unbind('change').on('change',function () { //类型点击事件
                //获取金额填入
                getProd($(this).val());
            });
        }
    }
    var proArr = [];
    $.each(project_data.project, function(id, name){
        proArr.push({id: id, text: name});
    });
    $('#project').select2({
        placeholder: '选择项目',
        data: proArr,
        allowClear: true
    });

    function getProd(id,$value) {
        $.ajax({
            url:baseUrl+'device/getChargeData',
            method:'post',
            data:{project:id,model:$('#model').val()},
            dataType:'json',
            success:function (data) {
                document.getElementById("money").value=data;
            }
        });
    }

    //上传附件
    var progress = $('#progress');
    uploadReport();
    function uploadReport()
    {
        var checkReport = $('#checkReport');
        checkReport.on('change', function(){
            progress.val('').attr('placeholder', '上传中，请稍后...');
            $.ajaxFileUpload({
                url: baseUrl + 'device/updateCheckFile',
                secureuri:false,
                fileElementId: checkReport[0].id,                               //file标签的id  
                dataType: 'json',                                               //返回数据的类型  
                success: function (data, status) {
                    if(data.error) {
                        window.top.xhTop.showMessage({
                            title: data.error,
                            type: 'error'
                        });
                        return false;
                    }
                    progress.val('上传完成');
                    $('#report_url').val(data.msg);
                    uploadReport();
                },
                error: function (data, status, e) {
                    window.top.xhTop.showMessage({
                        title: e,
                        type: 'error'
                    });
                    uploadReport();
                }
            });
        });
    }

    $('#bt_modal_confirm',parent.document).click(function () {
        var ids = $('#id_data').val();
        var type = $('#type_data').val();
        var status = $('#status_data').val();
        var report_url = $('#report_url').val();
        var project = $('#project').val();
        var chectContent = $('#chectContent').val();
        if (report_url < 1 || project < 1 || chectContent < 1 ){
            window.top.xhTop.showMessage({
                title: '请填写/上传完整信息',
                type: 'error'
            });
            xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
            return false;
        }

        xhExtra.ajaxRequest('Device/changeStatus', {ids:ids, type:type, status:status, checkReport:report_url, project:project,money:$('#money').val(),chectContent:chectContent},
            {success: true,check:false},function (res) {
                if(res.code === 200){
                    window.top.swal('成功',res.msg,'success');
                    window.top.xhTop.closeModal();
                    window.top.xhTop.getActiveFrame().obj.dataGrid.reload();
                } else {
                    window.top.xhTop.showError(res.error);
                    xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                }
            });
    });
});
